package com.soil.code.generation.database.mysql.repository;

import com.mybatisflex.core.MybatisFlexBootstrap;
import com.soil.code.generation.database.factory.DatabaseInfoRepositoryFactory;
import com.soil.code.generation.database.factory.FlexBootstrapFactory;
import com.soil.code.generation.database.mysql.repository.mapper.MysqlColumnsViewMapper;
import com.soil.code.generation.database.mysql.repository.mapper.MysqlTablesViewMapper;
import com.soil.code.generation.database.repository.ColumnInfoRepository;
import com.soil.code.generation.database.repository.TableInfoRepository;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * 数据库信息库工厂
 * @author zeno
 */
public class MysqlDatabaseInfoRepositoryFactory implements DatabaseInfoRepositoryFactory{

  private MybatisFlexBootstrap flexBootstrap;

  public void init( DataSource dataSource ){
    try{
      flexBootstrap = FlexBootstrapFactory.createFlexBootstrap(dataSource,
        "classpath:/mapper/*.xml",
        "com.soil.code.generation.database.mysql.mapper");
    } catch(IOException e){
      throw new RuntimeException(e);
    }
  }

  public ColumnInfoRepository createColumnInfoRepository(){
    MysqlColumnsViewMapper mapper = flexBootstrap.getMapper(MysqlColumnsViewMapper.class);
    return new MysqlColumnInfoRepository(mapper);
  }

  @Override
  public String type(){
    return "Mysql";
  }

  public TableInfoRepository createTableInfoRepository(){
    MysqlTablesViewMapper mapper = flexBootstrap.getMapper(MysqlTablesViewMapper.class);
    return new MysqlTableInfoRepository(mapper);
  }

}
